use std::{f32::consts::PI, time::Duration};

use bevy::{prelude::*, sprite::Anchor};

use crate::{
    animate::{Animator, LoopStrategy}, loader::GameSource
};

pub fn root_class(mut sprite: Mut<Sprite>, mut transform: Mut<Transform>) {
    sprite.custom_size = Some(Vec2::new(1080., 720.));
    sprite.color = Color::rgba(153. / 255., 153. / 255., 153. / 255., 1.);
    transform.translation = Vec3::new(0.0, 0.0, 0.0);
}

pub fn tank_selector(
    mut atlas: Mut<TextureAtlas>,
    mut image: Mut<Handle<Image>>,
    mut sprite: Mut<Sprite>,
    mut transform: Mut<Transform>,
    mut animator: Mut<Animator>,
    gm_res: Res<GameSource>,
) {
    atlas.layout = gm_res.tank_layout.clone();
    *image = gm_res.p0.clone();
    sprite.custom_size = Some(Vec2::new(48., 48.));
    sprite.anchor = Anchor::CenterLeft;
    transform.translation = Vec3::new(-120., 60., 0.);
    animator
        .set_loop_strategy(LoopStrategy::Once)
        .set_pause(true)
        .add_change()
        .set_retain_change(true)
        .set_duration(Duration::from_millis(300))
        .set_transform_translation(Vec3::new(0., 90., 0.));
    animator
        .add_change()
        .set_retain_change(true)
        .set_duration(Duration::from_millis(300))
        .set_transform_translation(Vec3::new(0., -90., 0.));
    animator
        .add_change()
        .set_retain_change(true)
        .set_duration(Duration::from_millis(300))
        .set_transform_translation(Vec3::new(0., 180., 0.));
    animator
        .add_change()
        .set_retain_change(true)
        .set_duration(Duration::from_millis(300))
        .set_transform_translation(Vec3::new(0., -180., 0.));
}

pub fn enter_top_brand(
    mut image: Mut<Handle<Image>>,
    mut sprite: Mut<Sprite>,
    mut transform: Mut<Transform>,
    gm_res: Res<GameSource>,
) {
    *image = gm_res.logo.clone();
    sprite.custom_size = Some(Vec2::new(450.0, 120.0));
    transform.translation = Vec3::new(0.0, 180.0, 1.0);
}

pub fn menu_text_style(mut text: Mut<Text>, gm_res: Res<GameSource>) {
    text.sections[0].style.font = gm_res.font.clone();
    text.sections[0].style.font_size = 22.0;
    text.sections[0].style.color = Color::WHITE;
}

pub fn player1_text(mut text_anchor: Mut<Anchor>, mut transform: Mut<Transform>) {
    *text_anchor = Anchor::CenterLeft;
    transform.translation = Vec3::new(-60.0, 60.0, 0.0);
}

pub fn player2_text(mut text_anchor: Mut<Anchor>, mut transform: Mut<Transform>) {
    *text_anchor = Anchor::CenterLeft;
    transform.translation = Vec3::new(-60.0, -30.0, 0.0);
}

pub fn map_editor_text(mut text_anchor: Mut<Anchor>, mut transform: Mut<Transform>) {
    *text_anchor = Anchor::CenterLeft;
    transform.translation = Vec3::new(-60.0, -120.0, 0.0);
}

pub fn editor_root(mut sprite: Mut<Sprite>) {
    sprite.color = Color::rgb_u8(19, 42, 63);
    sprite.custom_size = Some(Vec2::new(1080., 720.));
}

pub fn left_panel(mut style: Mut<Style>, mut background: Mut<BackgroundColor>) {
    style.width = Val::Px(150.);
    style.height = Val::Px(720.);
    style.display = Display::Flex;
    style.flex_direction = FlexDirection::Column;
    background.0 = Color::rgb_u8(30, 34, 41);
}



pub fn game_panel(
    mut sprite: Mut<Sprite>,
    mut image: Mut<Handle<Image>>,
    mut transform: Mut<Transform>,
    mut animator: Mut<Animator>,
    gm_res: Res<GameSource>,
) {
    *image = gm_res.world.clone();
    transform.translation = Vec3::new(0.0, 100.0, 0.0);
    sprite.custom_size = Some(Vec2::new(630., 630.));
    animator
        .set_loop_strategy(LoopStrategy::AllOnce)
        .add_change()
        .set_duration(Duration::from_millis(500));
    animator
        .add_change()
        .set_duration(Duration::from_secs(1))
        .set_transform_translation(Vec3::new(0.0, -100.0, 0.0));
}

pub fn despawn_screen<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
    for entity in &to_despawn {
        commands.entity(entity).despawn_recursive();
    }
}

/* ----------------- */

pub fn rotate_180(mut animator: Mut<Animator>) {
    animator.set_loop_strategy(LoopStrategy::LoopAll);
    animator
        .add_change()
        .set_retain_change(true)
        .set_duration(Duration::from_millis(500))
        .set_transform_rotation(Quat::from_rotation_z(PI));
    animator
        .add_change()
        //.set_retain_change(true)
        .set_duration(Duration::from_millis(500))
        .set_transform_rotation(-Quat::from_rotation_z(PI));
}
